package com.it.controller;

import com.it.entity.User;
import com.it.pojo.Result;
import com.it.service.IUserService;
import com.it.service.ex.InsertException;
import com.it.service.ex.UsernameDuplicatedException;
import com.it.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.it.controller.BaseController.OK;

//@Controller
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/reg")
   public JsonResult<Void> reg(User user){
        JsonResult<Void> result=new JsonResult<>();
        try {
            userService.reg(user);
            result.setState(200);
            result.setMessage("用户注册成功");
        } catch (UsernameDuplicatedException e) {
            result.setState(4000);
            result.setMessage("用户名被占用");
        } catch (InsertException e) {
            result.setState(5000);
            result.setMessage("注册时产生未知的异常");
        }
        return  result;
    }
//    public JsonResult<Void> reg(User user){
//
//        userService.reg(user);
//
//        return  new JsonResult<>(OK);
//    }


}
